﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected string resultMessage = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string username = Session["username"] as string;
                if (username != null)
                {
                    this.lblTitle.Text = "修改用户[" + username + "]的密码" ;
                }
            }
        }

        private void update(User user)
        {
            try
            {
                if (user != null)
                {
                    Users_Dao dao = new Users_Dao();
                    int result = dao.ChangePassword(user.Id.ToString(), this.tbNewPassword.Text);
                    if (result == 1)
                    {
                        resultMessage = "更新密码成功";
                    }
                }
                else
                {
                    resultMessage = "请先登录";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "更新密码失败:" + ex.Message;
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            User user = Session["user"] as User;
            if (this.tbPassword.Text.Equals(user.Password))
            {
                update(user);
            }
            else
            {
                resultMessage = "错误的旧密码";
            }
            this.lbResult.Text = resultMessage;
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            this.tbPassword.Text = "";
            this.tbNewPassword.Text = "";
            this.tbConfirmPassword.Text = "";
        }
    }
}